<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button">
            <span>{{this.city}}</span>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button"
            v-for="item of hot"
            :key="item.id"
            @click="handleCityClick(item.name)"
          >
            <span>{{item.name}}</span>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <ul>
          <li class="border-bottom"
              v-for="innerItem of item"
              :key="innerItem.id"
              @click="handleCityClick(innerItem.name)"
          >{{innerItem.name}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    handleCityClick (city) {
      // this.$store.commit('changeCity', city)
      this.currentChangeCity(city)
      this.$router.push('/')
    },
    ...mapMutations({
      currentChangeCity: 'changeCity'
    })
    // ...mapMutations(['changeCity'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        const el = this.$refs[this.letter][0]
        this.scroll.scrollToElement(el)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl';
.title{
  line-height .5rem
  background #eee
  padding-left .15rem
  font-size .3rem
  color #666
  &:before, &:after{
    border-color #ccc
  }
}
.list{
  overflow hidden
  position absolute
  top 1.58rem
  left 0
  right 0
  bottom 0
  .button-list{
    width 100%
    overflow hidden
    padding .1rem .6rem .1rem .1rem
    box-sizing border-box
    .button{
      width 33.33%
      float left
      span{
        line-height .4rem
        border .02rem solid #ccc
        padding .05rem 0
        text-align center
        box-sizing border-box
        margin .1rem
        border-radius .1rem
        display block
      }
    }
  }
  ul{
    li{
      line-height .76rem
      color #666
      padding-left .2rem
      &:before {
        border-color #ccc
      }
    }
  }
}

</style>
